package ui;
import java.util.Scanner;
import ctrl.*;
import model.*;

public class DVDUI
{
    // instance variables
    private DVDCtrl dvdCtrl;

    /**
     * Constructor for objects of class DVDUI
     */
    public DVDUI()
    {
        // initialise instance variables
        dvdCtrl = new DVDCtrl();
    }

    /**
     * Den starter DVD menuen op.
     */
    public void Start()
    {
        DVDMenu();
    }

    /**
     * Skriver dvd menu og laver svar muligheder.
     */
    private void DVDMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeDVDMenu(); 
            switch(choice)
            {
                case 1:
                createDVD();
                break;
                case 2:
                DVD d = findDVD();
                if(d == null)
                {
                    System.out.println("DVD blev ikke fundet!");
                }
                else
                {
                    System.out.println(" DVD har titlen: " + d.getTitle() +"\n");
                }           
                break;
                case 3:
                listAllDVDs();
                break;
                case 4:

                updateMenu();
                break;
                case 5:
                removeDVD();
                break;
                case 6:
                createCopy();
                break;
                case 7:
                Copy c = findCopy();
                if(c == null)
                {
                    System.out.println("Kopien blev ikke fundet!");
                }
                else
                {
                    System.out.println(" Kopi har serienummeret: " + c.getSerialNumber() +"\n");
                }     
                break;
                case 8:
                updateCopyMenu();
                break;
                case 9:
                removeCopy();    
                break;
                default:
                exit = true;            
            }
        }
    }

    /**
     * set metoder som returner en værdi.
     * @return title
     */
    private String inputTitle()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv titel: ");
        String title = keyboard.nextLine();
        return title;
    }

    /**
     * set metoder som returner en værdi.
     * @return barcode
     */
    private String inputBarcode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv UPC-nummer: ");
        String barcode = keyboard.nextLine();
        return barcode;
    }

    /**
     * set metoder som returner en værdi.
     * @return artist
     */
    private String inputArtist()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv forfatter: ");
        String artist = keyboard.nextLine();
        return artist;
    }

    /**
     * set metoder som returner en værdi.
     * @return publicationDate
     */
    private String inputPublicationDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv udgivelsesdato: ");
        String publicationDate = keyboard.nextLine();
        return publicationDate;
    }

    /**
     * set metoder som returner en værdi.
     * @return serialNumber
     */
    private String inputSerialNumber()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv serienummer: ");
        String serialNumber = keyboard.nextLine();
        return serialNumber;
    }

    /**
     * set metoder som returner en værdi.
     * @return purchaseDate
     */
    private String inputPurchaseDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv købsdato: ");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }

    /**
     * set metoder som returner en værdi.
     * @return purchasePrice
     */
    private String inputPurchasePrice()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv indkøbspris: ");
        String purchasePrice = keyboard.nextLine();
        return purchasePrice;
    }

    /**
     * printer UI'en ud i konsollen.
     * @return choice
     */
    private int writeDVDMenu()
    {      
        // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** DVD Menu ***");
        System.out.println(" (1) Opret DVD");
        System.out.println(" (2) Find DVD");
        System.out.println(" (3) List alle DVD'er");
        System.out.println(" (4) Opdater DVD");
        System.out.println(" (5) Fjern DVD");
        System.out.println(" (6) Opret kopi");
        System.out.println(" (7) Find kopi");
        System.out.println(" (8) Opdater kopi");
        System.out.println(" (9) Fjern kopi");
        System.out.println(" (10) Back");
        System.out.print("\n Vaelg: ");
        int choise;

        while(!keyboard.hasNextInt()){
            System.out.println("Indtast venligst et tal ");
            keyboard.next(); 
        }
        choise = keyboard.nextInt();
        System.out.print("\n");
        return choise;
    }

    /**
     * henter metoden createDVD fra dvdCtrl
     */
    private void createDVD()
    {
        String title = inputTitle();
        String barcode = inputBarcode();
        String artist = inputArtist();
        String publicationDate = inputPublicationDate();
        dvdCtrl.createDVD(barcode, title, artist, publicationDate);

    }

    /**
     * Henter metoden findDVD fra DVD
     * @return d
     */
    private DVD findDVD()
    {
        String title = inputTitle();
        DVD d = null;
        d = dvdCtrl.findDVD(title);
        return d;
    }

    /**
     * henter metoden listAllDVDs fra dvdCtrl
     */
    private void listAllDVDs()
    {
        dvdCtrl.listAllDVDs();
    }

    /**
     * henter metoden findDVD fra DVD
     * opdatere DVD'ens værdier og sender en fejlbesked hvis DVD'en ikke blev fandt.
     */
    private void updateMenu()
    {
        boolean back = false;
        DVD d = findDVD();
        if(d != null)
        {
            while(!back)//that is: while exit is false
            {
                int choice = writeUpdateDVDMenu(); 
                switch(choice)
                {
                    case 1:
                    String barcode = inputBarcode();
                    d.setBarCode(barcode);
                    break;
                    case 2:
                    String title = inputTitle();
                    d.setTitle(title);
                    break;
                    case 3:
                    String artist = inputArtist();
                    d.setArtist(artist);
                    break;
                    case 4:
                    String publicationDate = inputPublicationDate();
                    d.setPublicationDate(publicationDate);
                    break;
                    default:
                    back = true;            
                }
            }
        }
        else
        {
            System.out.println("Der blev ikke fundet en DVD med den titel \n");
        }
        Start();
    }

    /**
     * printer de tilgængelige metoder ud i konsollen
     * @return choice
     */
    private int writeUpdateDVDMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** DVD Update Menu ***");
        System.out.println(" (1) Rediger barcode");
        System.out.println(" (2) Rediger title");
        System.out.println(" (3) Rediger artist");
        System.out.println(" (4) Rediger publicationDate");
        System.out.println(" (5) Back");
        System.out.print("\n Vaelg: ");
        while(!keyboard.hasNextInt()){
            System.out.println("Indtast venligst et tal ");
            keyboard.next(); 
        }
        int choise = keyboard.nextInt();
        return choise;
    }

    /** 
     * Fjerner en dvd fra listen med metoden removeDVD som den henter fra dvdCtrl
     */
    private void removeDVD()
    {
        String title = inputTitle();
        if(dvdCtrl.removeDVD(title) == true)
        {
            System.out.println(" DVD'en fjernes nu fra DVDlisten" + "\n");
        }
        else{
            System.out.println("DVD blev ikke fundet");
        }
    }

    /**
     * finder en ny copy fra DVD
     */
    private void createCopy()
    {
        String title = inputTitle();
        DVD d = dvdCtrl.findDVD(title);
        if(dvdCtrl.findDVD(title) == null){
            System.out.println("DVD'en med den titel findes ikke " + "\n");
        }
        else
        {
            String serialNumber = inputSerialNumber();
            String purchaseDate = inputPurchaseDate();
            String purchasePrice = inputPurchasePrice();

            dvdCtrl.createCopy(d, serialNumber, purchaseDate, purchasePrice);
            System.out.println("Kopien'en er tilføjet " + "\n");
        }

    }

    /**
     * henter metoden findCopy fra Copy
     * @return c

     */
    private Copy findCopy()
    {
        String serialNumber = inputSerialNumber();
        Copy c = dvdCtrl.findCopy(serialNumber);
        return c;
    }

    /**
     * opdatere Copy menuen
     */
    private void updateCopyMenu()
    {
        boolean back = false;
        Copy c = findCopy();
        if(c != null)
        {
            while(!back)//that is: while exit is false
            {
                int choice = writeUpdateCopyMenu(); 
                switch(choice)
                {
                    case 1:
                    String serialNumber = inputSerialNumber();
                    c.setSerialNumber(serialNumber);
                    break;
                    case 2:
                    String purchaseDate = inputPurchaseDate();
                    c.setPurchaseDate(purchaseDate);
                    break;
                    case 3:
                    String purchasePrice = inputPurchasePrice();
                    c.setPurchasePrice(purchasePrice);
                    break;
                    default:
                    back = true;            
                }
            }
        }
        Start();
    }

    /**
     * printer en liste ud med valgmuligheder inden for Copy menuen
     * @return choice
     */
    private int writeUpdateCopyMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Copy Update Menu ***");
        System.out.println(" (1) Rediger serienummer");
        System.out.println(" (2) Rediger indkøbsdato");
        System.out.println(" (3) Rediger indkøbspris");
        System.out.println(" (4) Back");
        System.out.print("\n Vaelg: ");
        while(!keyboard.hasNextInt()){
            System.out.println("Indtast venligst et tal ");
            keyboard.next(); 
        }
        int choise = keyboard.nextInt();
        return choise;
    }

    /**
     * henter metoden removeCopy fra Copy
     */
    private void removeCopy()
    {
        String serialNumber = inputSerialNumber();
        if(dvdCtrl.removeCopy(serialNumber) == true)
        {
            System.out.println("Kopien er fjernet. " + "\n");
        }
        else
        {
            System.out.println("Kopi med det serienummer blev ikke fundet " + "\n");
        }
    }
}
